package datastructure;

import java.util.ArrayList;

public class ListNode {

    public int val;
    public ListNode next;
    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    /**
     * 遍历打印listNode
     * @param listNode
     */
    public static void printListNode(ListNode listNode) {
        ArrayList<Integer> nums = new ArrayList<>();
        while (listNode != null) {
            nums.add(listNode.val);
            listNode = listNode.next;
        }
        System.out.println(nums);
    }

    /**
     * 通过数组组装链表
     * @param ints
     * @return
     */
    public static ListNode buildList(int[] ints) {

        ListNode listNode = null;
        if (ints.length > 0) {
            listNode = new ListNode(ints[0]);
        }
        ListNode head = listNode;
        for (int i = 1; i < ints.length; i++) {
            listNode.next = new ListNode(ints[i]);
            listNode = listNode.next;
        }
        return head;
    }

}
